home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
God is Closer Than You Think
/
God is Closer.iso
/
mac
/
assets
/
tweenManager.as
< prev
next >
Wrap
Text File
|
2005-01-20
|
5KB
|
197 lines
╘¬°/*
class tweenManager for tweening prototypes
version 1.0
Ladislav Zigo,lacoz@inmail.sk
*/
class tweenManager{
static private var tweenList:Array = new Array();
static private var tweenHolder:MovieClip = _root.createEmptyMovieClip("_th_",6789);
static private var playing:Boolean = false;
static private var now:Number;
// private methods
static private function init():Void{
tweenHolder.onEnterFrame = update;
playing = true;
now = getTimer();
}
static private function deinit():Void{
playing = false;
delete tweenHolder.onEnterFrame;
}
static private function update():Void{
// loop all tween objects
var i = tweenList.length;
while (i--){
var t = tweenList[i];
if(t.ts > now){
//trace("wait");
// wait, next iteration
continue;
}
if(t.ts + t.d > now){
// compute value using equation function
if(t.ctm == undefined){
// compute primitive value
t.mc[t.pp] = t.ef(now-t.ts,t.ps,t.ch,t.d,t.e1,t.e2);
}else{
// compute color transform matrix
// stm is starting transform matrix,
// ctm is change in start & destination matrix
// ttm is computed (temporary) transform matrix
// c is color object
var ttm = {};
for(var j in t.ctm){
ttm[j] = t.ef(now-t.ts,t.stm[j],t.ctm[j],t.d,t.e1,t.e2)
}
t.c.setTransform(ttm);
}
} else {
// end , set up the property to end value;
if(t.ctm == undefined){
t.mc[t.pp] = t.ps + t.ch;
} else {
var ttm = {};
for(var j in t.ctm){
ttm[j] = t.stm[j]+t.ctm[j]
}
t.c.setTransform(ttm);
}
endTween(i);
}
}
// update timer
now = getTimer();
}
static private function endTween(id:Number):Void{
//callback function
var et = tweenList[id];
if(et.cb != undefined){
et.cb.func.apply(et.cb.scope,et.cb.args);
if(tweenList[id] != et){
// there was callback function changed the tweenList
// we must find again the id
var i = tweenList.length
while(i--){
if (tweenList[i] == et){
id = i
break;
}
}
}
}
tweenList.splice(id,1);
//
if(tweenList.length==0){
// last tween removed, erase onenterframe function
deinit();
}
}
// public methods
static public function addTween(mc:MovieClip,props:Array,pEnd:Array,
sec:Number,delay:Number,eqFunc:Function,
callback:Object,extra1:Number,extra2:Number):Void{
if(!playing){
init();
}
//
for(var i in props){
if(props[i].substr(0,4)!="_ct_"){
// there is no color transform prefix, use primitive value tween
tweenList.unshift({
mc: mc, // reference to movieclip
pp: props[i], // property
ps: mc[props[i]], // starting value of property
ch: pEnd[i] - mc[props[i]], // difference between starting and end value
ts: now + delay * 1000, // start time of tween
d: sec * 1000, // duration of tween
ef: eqFunc, // reference to easing equation function
cb: callback, // callback object (function which is called at end of tween)
e1: extra1, // extra 1 value (for elastic and bouce equation)
e2: extra2}); // extra 2 value (for elastic equation)
}else{
// color trasform prefix found
// compute change matrix
var c = new Color(mc);
var stm = c.getTransform();
// compute difference between starting and desionation matrix
var ctm = {}
for(var j in pEnd[i]){
// if is in destination matrix
if(pEnd[i][j] != stm[j] ){
ctm [j] = pEnd[i][j] - stm[j]
}
}
// if there is no difference between start & end do not perform tween
//if()
tweenList.unshift({
mc: mc, //reference to movieclip
c: c, //reference to movieclip color
stm: stm, //starting transform matrix
ctm: ctm,
ts: now + delay * 1000,
d: sec * 1000,
ef: eqFunc,
cb: callback,
e1: extra1,
e2: extra2
})
}
}
}
static public function removeTween(mc:MovieClip,props:Array):Void{
var all = false;
if(props == undefined){
// props are undefined, remove all tweens
var all = true;
}
var i = tweenList.length;
while (i--){
if(tweenList[i].mc == mc){
if(all){
tweenList.splice(i,1);
}else{
for(var j in props){
if(tweenList[i].pp == props[j]){
tweenList.splice(i,1);
// props.splice(j,1)
// (because allows add same properties for same mc,
// all tweens must be checked)
}
}
}
}
}
if(tweenList.length==0){
// last tween removed, erase onenterframe function
deinit();
}
}
static public function isTweening(mc:MovieClip):Boolean{
var is = false;
for (var i in tweenList){
if(tweenList[i].mc == mc){
// mc found, so break loop
is = true;
break;
}
}
return is;
}
static public function getTweens(mc:MovieClip):Number{
var count = 0;
for (var i in tweenList){
if(tweenList[i].mc == mc){
// found, increase count
count++;
}
}
return count;
}
}